package com.wuxl;


import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameRecorder;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:  图片合成MP4
 * https://cloud.tencent.com/developer/article/1640244
 * https://cloud.tencent.com/developer/article/1655887
 *
 * @author 诸葛小猿
 * @date 2020-10-24
 */
public class Image2Mp4 {


    public static void main(String[] args) throws Exception {
        //合成的MP4
        String mp4SavePath = "C:\\Users\\WuXiaoLong\\Desktop\\img\\img.mp4";
        //图片地址 这里面放了22张图片
        String img = "C:\\Users\\WuXiaoLong\\Desktop\\img";
        int width = 2600;
        int height = 2900;
        //读取所有图片
        File file = new File(img);
        File[] files = file.listFiles();
        Map<Integer, File> imgMap = new HashMap<Integer, File>();
        int num = 0;
        for (File imgFile : files) {
            imgMap.put(num, imgFile);
            num++;
        }
        createMp4(mp4SavePath, imgMap, width, height);
    }

    private static void createMp4(String mp4SavePath, Map<Integer, File> imgMap, int width, int height) throws FrameRecorder.Exception {
        //视频宽高最好是按照常见的视频的宽高  16：9  或者 9：16
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(mp4SavePath, width, height);
        //设置视频编码层模式
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        //设置视频为25帧每秒
        recorder.setFrameRate(25);
        //设置视频图像数据格式
        recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
        recorder.setFormat("mp4");
        try {
            recorder.start();
            Java2DFrameConverter converter = new Java2DFrameConverter();
            //录制一个22秒的视频
            for (int i = 0; i < imgMap.size(); i++) {
                BufferedImage bufferedImage  = ImageIO.read(imgMap.get(i));

                System.out.println(bufferedImage.getType());
                if(bufferedImage.getType()==BufferedImage.TYPE_4BYTE_ABGR){
                    continue;
                }

//                if(bufferedImage.getType()==BufferedImage.TYPE_4BYTE_ABGR){
//                    //开始转换
//                    BufferedImage image = new BufferedImage(
//                            bufferedImage.getWidth(),
//                            bufferedImage.getHeight(),
//                            BufferedImage.TYPE_3BYTE_BGR);
//                    for (int y = 0; y < bufferedImage.getHeight(); y++) {
//                        for (int x = 0; x < bufferedImage.getWidth(); x++) {
//                            int argb = bufferedImage.getRGB(x, y);
//                            image.setRGB(x, y, argb & 0xFF000000); // same color alpha 100%
//                        }
//                    }
//                    bufferedImage = image;
//                }


                //一秒是25帧 所以要记录25次
                for (int j = 0; j < 25; j++) {
                    recorder.record(converter.getFrame(bufferedImage));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //最后一定要结束并释放资源
            recorder.stop();
            recorder.release();
        }
    }

}
